package com.ls.pattern.chain.simple;


import java.util.ArrayList;
import java.util.List;

public class Client {

    public String register(String username, String password, int age) {
        List<AbstractHandler> list = new ArrayList<>();
        list.add(new UserNameHandler());
        list.add(new AgeHandler());

        Context context = new Context();
        context.setUsername(username);
        context.setPassword(password);
        context.setAge(age);

        boolean result = false;
        for (AbstractHandler handler : list) {
            result = handler.doHandle(context);
            if (!result) {
                break;
            }
        }
        return result ? "注册成功" : "注册失败";
    }

    //    public String register(String username, String password, int age) {
//        AbstractHandler handler = new UserNameHandler()
//                .setNextHandler(new AgeHandler());
//
//        Context context = new Context();
//        context.setUsername(username);
//        context.setPassword(password);
//        context.setAge(age);
//
//        boolean handle = handler.handle(context);
//        return handle ? "注册成功" : "注册失败";
//    }

    public static void main(String[] args) {
        Client client = new Client();
        System.out.println(client.register("张三", "123456", 20));
        System.out.println("------------");
        System.out.println(client.register("张三", "123456", 17));
        System.out.println("------------");
        System.out.println(client.register("", "123456", 20));
        System.out.println("------------");
        System.out.println(client.register("张三", "", 20));
    }
}
